'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async list() {
    const { ctx } = this;
    try {
      const userList = await ctx.service.user.searchAll();
      ctx.body = {
        success: true,
        data: userList
      };
    } catch (error) {
      ctx.body = {
        success: false,
        error
      };
    }
  }


  async login() {
    const { ctx } = this;
    try {
      if (!ctx.query.code) throw new Error('缺少参数！');
      console.log(ctx.query.password);
      const haveCode = await ctx.service.user.findCode(ctx.query.code);
      /* 账号验证通过 */
      if (haveCode.result) {
        // console.log(haveCode.result.password)
        if (haveCode.result.password == ctx.query.password) {
          ctx.body = {
            code: 200,
            data: haveCode,
            msg: '登录成功！'
          }
        } else {
          ctx.body = {
            code: -1,
            data: null,
            msg: '密码错误'
          }
        }
      }
    } catch (error) {
      ctx.body = {
        code: -1,
        data: haveCode,
        msg: '没有账号，请联系工作人员'
      }
    }



  }
}



module.exports = UserController;
